package com.gitee.zeric.codec.check;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

/**
 * 默认唯一性校验实现类(redis bitmap实现，会有一定误判率 0.01)
 * @author zj
 * @date 2022/4/19
 */
@ConditionalOnProperty(prefix = "dps",name = "unichecker",havingValue = "redis")
@Component
@RequiredArgsConstructor
public class RedisCodeUniqueCheckImpl implements CodeUniqueCheck {

    private final RedisBloomFilter redisBloomFilter;

    private final BloomFilterHelper bloomFilterHelper;

    @Override
    public boolean checkAndInsert(String ruleCode, String realCode) {
        boolean flag = false;
        String bitKey = "zcode:bit:" + ruleCode;
        if (!redisBloomFilter.includeByBloomFilter(bloomFilterHelper, bitKey, realCode)) {
            redisBloomFilter.addByBloomFilter(bloomFilterHelper,bitKey, realCode);
            flag = true;
        }
        return flag;
    }
}
